knitr::opts_chunk$set(echo = TRUE)
library(readr)
library(ggplot2)
sparrows <- read_csv("~/Documents/Courses/Multivariate Analysis/Bumpus_sparrows.csv")
## Rows: 49 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Survivorship
## dbl (5): Total_length, Alar_extent, L_beak_head, L_humerous, L_keel_sternum
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(sparrows)
## spc_tbl_ [49 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Survivorship  : chr [1:49] "S" "S" "S" "S" ...
##  $ Total_length  : num [1:49] 156 154 153 153 155 163 157 155 164 158 ...
##  $ Alar_extent   : num [1:49] 245 240 240 236 243 247 238 239 248 238 ...
##  $ L_beak_head   : num [1:49] 31.6 30.4 31 30.9 31.5 32 30.9 32.8 32.7 31 ...
##  $ L_humerous    : num [1:49] 18.5 17.9 18.4 17.7 18.6 19 18.4 18.6 19.1 18.8 ...
##  $ L_keel_sternum: num [1:49] 20.5 19.6 20.6 20.2 20.3 20.9 20.2 21.2 21.1 22 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Survivorship = col_character(),
##   ..   Total_length = col_double(),
##   ..   Alar_extent = col_double(),
##   ..   L_beak_head = col_double(),
##   ..   L_humerous = col_double(),
##   ..   L_keel_sternum = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
boxplot(sparrows[,2:6])

stars(sparrows,labels = sparrows$Survivorship)

library(SciViews)
library(scatterplot3d)
library(car)
## Loading required package: carData
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(lattice)
library(ggplot2)
library(ggridges)
library(ggvis)
## 
## Attaching package: 'ggvis'
## 
## The following object is masked from 'package:ggplot2':
## 
##     resolution
library(ggthemes)
library(cowplot)
## 
## Attaching package: 'cowplot'
## 
## The following object is masked from 'package:ggthemes':
## 
##     theme_map
library(gapminder)
library(gganimate)
## No renderer backend detected. gganimate will default to writing frames to separate files
## Consider installing:
## - the `gifski` package for gif output
## - the `av` package for video output
## and restarting the R session
## 
## Attaching package: 'gganimate'
## 
## The following object is masked from 'package:ggvis':
## 
##     view_static
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following object is masked from 'package:car':
## 
##     recode
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble  3.1.8     ✔ stringr 1.5.0
## ✔ tidyr   1.3.0     ✔ forcats 1.0.0
## ✔ purrr   1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()     masks stats::filter()
## ✖ dplyr::lag()        masks stats::lag()
## ✖ dplyr::recode()     masks car::recode()
## ✖ ggvis::resolution() masks ggplot2::resolution()
## ✖ purrr::some()       masks car::some()
library(grid)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(RColorBrewer)
library(hexbin)
attach(sparrows)
names(sparrows)
## [1] "Survivorship"   "Total_length"   "Alar_extent"    "L_beak_head"   
## [5] "L_humerous"     "L_keel_sternum"
ggscatmat(sparrows, columns=2:6, color="Survivorship")
## Warning: The dot-dot notation (`..scaled..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(scaled)` instead.
## ℹ The deprecated feature was likely used in the GGally package.
##   Please report the issue at <]8;;https://github.com/ggobi/ggally/issueshttps://github.com/ggobi/ggally/issues]8;;>.

# ggplot
#Using ggplot to plot different variables against survivorship of birds
ggplot(sparrows, aes(x=Survivorship,y=Total_length)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship,y=Alar_extent)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship,y=L_beak_head)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship,y=L_humerous)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship,y=L_keel_sternum)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Total_length,y=Survivorship)) + facet_wrap(Alar_extent) + geom_point()

ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Total_length,y=Alar_extent)) + xlim(145,170) + geom_point(aes(color=Survivorship), pch=17) + 
  labs(x="Total length of Bird", y="Length of Alar Extent", title="Sparrow Data") 

# bar chart
ggplot(sparrows, aes(Total_length)) + geom_bar(position="stack",aes(color=Survivorship)) 

ggplot(sparrows, aes(Alar_extent)) + geom_bar(position="stack",aes(color=Survivorship))

ggplot(sparrows, aes(L_beak_head)) + geom_bar(position="stack",aes(color=Survivorship))

ggplot(sparrows, aes(L_humerous)) + geom_bar(position="stack",aes(color=Survivorship))

ggplot(sparrows, aes(L_keel_sternum)) + geom_bar(position="stack",aes(color=Survivorship))

ggplot(sparrows, aes(Total_length)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

ggplot(sparrows, aes(Alar_extent)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

ggplot(sparrows, aes(L_beak_head)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

ggplot(sparrows, aes(L_humerous)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

ggplot(sparrows, aes(L_keel_sternum)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

# histogram
ggplot(sparrows, aes(Total_length))+geom_histogram(aes(color=Survivorship))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(sparrows, aes(Alar_extent))+geom_histogram(aes(fill = after_stat(count)))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(sparrows, aes(L_humerous))+geom_histogram(aes(fill = after_stat(count)))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# violin plot 
ggplot(sparrows, aes(x=Survivorship, y=Total_length)) + geom_violin(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=Alar_extent)) + geom_violin(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_beak_head)) + geom_violin(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_humerous)) + geom_violin(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_keel_sternum)) + geom_violin(aes(color=Survivorship))

# box plot
ggplot(sparrows, aes(x=Survivorship, y=Total_length)) + geom_boxplot(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=Alar_extent)) + geom_boxplot(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_beak_head)) + geom_boxplot(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_humerous)) + geom_boxplot(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_keel_sternum)) + geom_boxplot(aes(color=Survivorship))

# density plot and ggridges
ggplot(sparrows, aes(x=Total_length, fill=Survivorship, color=Survivorship)) + geom_density(alpha=0.3, aes(y=..scaled..)) 

ggplot(sparrows, aes(x=Total_length, y=Survivorship)) + geom_density_ridges(aes(fill=Survivorship, color=Survivorship))
## Picking joint bandwidth of 1.63

# hexbin
ggplot(sparrows, aes(x=Total_length, y=Survivorship)) + geom_hex(aes(color=Survivorship)) 

# with ggthemes (see also ggsci, ggthemr)
lastplot <- ggplot(sparrows, aes(x=Total_length,y=Alar_extent)) + xlim(145,170) + geom_point(aes(color=Survivorship), pch=18) + 
  labs(x="Total length of Bird", y="Length of Alar Extent", title="Sparrow Data") 

lastplot + theme_bw()

lastplot + theme_cowplot()

lastplot + theme_dark()

lastplot + theme_economist()

lastplot + theme_fivethirtyeight()

lastplot + theme_tufte()

lastplot + theme_wsj()

Inference: This markdown did not need any more amount of inferences as they all point out to the same thing. Hence I have created a overall inference of all the plots. From these compilation of plots, the common point is that the sparrows on the extreme ends on either side, are dead. All the sparrows which have survived lie in the middle range (i.e. average range) on the distribution.